package ${basePackage}.${moduleName}.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import ${basePackage}.${moduleName}.model.${className};
import ${basePackage}.${moduleName}.service.${className}Service;
import ${basePackage}.util.Page;
import ${basePackage}.util.ResultJsonUtil;

@RestController
public class ${className}Controller {
	@Autowired
	private ${className}Service ${lowerClassName}Service;
	
	@GetMapping("/${lowerClassName}/list")
	public Map<String,Object> search${className}(HttpServletRequest request) {
		Page page = Page.newBuilder(request);
		${lowerClassName}Service.search${className}(page.getSearchPageMap(request));
		return page.getPageReturn();
	}
	
	@GetMapping("/${lowerClassName}/{${primary}}")
	public Map<String,Object> get${className}(@PathVariable("${primary}")String ${primary}) {
		List<${className}> ${lowerClassName} = ${lowerClassName}Service.get${className}(${primary});
		Map<String,Object> result = ResultJsonUtil.getSuccessResult();
		result.put(ResponseDataUtil.DATA, ${lowerClassName});
		return result;
	}
	
	@PostMapping("/insert")
	public Map<String,Object> insert(@Valid ${className} ${lowerClassName},BindingResult binding) {
		if(binding.hasErrors()){
			return ResultJsonUtil.analyzeError(binding);
		}
		${lowerClassName}Service.insert(${lowerClassName});
		return ResultJsonUtil.getSuccessResult();
	}
	
	@PutMapping("/update")
	public Map<String,Object> update(@Valid ${className} ${lowerClassName},BindingResult binding) {
		if(binding.hasErrors()){
			return ResultJsonUtil.analyzeError(binding);
		}
		${lowerClassName}Service.update(${lowerClassName});
		return ResultJsonUtil.getSuccessResult();
	}
	
	@DeleteMapping("/delete")
	public Map<String,Object> delete(String ${primary}s) {
		if(${primary}s!=null){
			String[] _ids = ${primary}s.split(",");
			for(String ${primary} : _ids){
				if(!${primary}.equals("")){
					${lowerClassName}Service.delete(${primary});
				}
			}
		}
		return ResultJsonUtil.getSuccessResult();
	}



}
